package com.wanwu.channel.common.util;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;
import jodd.http.HttpResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xun
 * @version 1.0
 * @date 2019/1/25 3:59 PM
 * @project trade
 */
public class DingUtil {

    private static final Logger logger = LoggerFactory.getLogger(DingUtil.class);

    private static final String DING_API_URL = "https://oapi.dingtalk.com/robot/send?access_token=";

    private static final Map<String, String> headerMap = new HashMap<>();

    static {
        headerMap.put("Content-Type", "application/json");
    }

    /**
     * 发送钉钉消息，并@指定列表的人，或所有人
     * @param appKey
     * @param title
     * @param msg
     * @param mobiles
     */
    public static void sendDingAtPeople(String appKey, String title, String msg, List<String> mobiles){
        String hostName = HostUtils.getHostName();
        if (StringUtils.isNotBlank(hostName)){
            msg = msg + "\n\n机器名:" + hostName;
        }
        DingUtil.DingMsgVo dingMsgVo = new DingUtil.DingMsgVo();
        DingUtil.DingMsgVo.AtVo atVo = new DingUtil.DingMsgVo.AtVo();
        if(CollectionUtils.isEmpty(mobiles)){
            atVo.setIsAtAll(true);
        }else{
            atVo.setAtMobiles(mobiles);
        }
        dingMsgVo.setMarkdownMsg(title, "渠道系统报警：" + msg, atVo);
        DingUtil.sendMsg(Lists.newArrayList(appKey), dingMsgVo);
    }

    public static void sendMsg(String appKey, DingMsgVo dingMsgVo) {
        String jsonData = JSONObject.toJSONString(dingMsgVo);
        LogUtil.info(logger, "发送钉钉请求报文:{0}", jsonData);
        HttpResponse response = HttpUtil.postUTF8Tens(DING_API_URL + appKey, jsonData, headerMap);
        LogUtil.info(logger, "发送钉钉返回报文:{0}", response);
    }

    public static void sendMsg(List<String> appKeys, DingMsgVo dingMsgVo) {
        for (String appKey : appKeys) {
            try {
                sendMsg(appKey, dingMsgVo);
            } catch (Exception ex) {
                LogUtil.error(logger, ex, "发送钉钉消息异常,appKey:{0}", appKey);
            }
        }
    }

    /**
     * 发送钉钉报警并@所有人
     */
    public static void sendDTalkAll(String appKey,String title,String msg) {
        DingUtil.DingMsgVo dingMsgVo = new DingUtil.DingMsgVo();
        DingUtil.DingMsgVo.AtVo atVo = new DingUtil.DingMsgVo.AtVo();
        atVo.setIsAtAll(true);
        dingMsgVo.setMarkdownMsg(title, msg, atVo);
        DingUtil.sendMsg(appKey, dingMsgVo);
    }


    public static class DingMsgVo implements Serializable {
        private static final long serialVersionUID = -1344252619276885529L;

        private String msgtype = "markdown";
        private MarkdownMsgVo markdown;
        private TextMsgVo text;
        private AtVo at;

        public String getMsgtype() {
            return msgtype;
        }

        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }

        public MarkdownMsgVo getMarkdown() {
            return markdown;
        }

        public void setMarkdown(MarkdownMsgVo markdown) {
            this.markdown = markdown;
        }

        public TextMsgVo getText() {
            return text;
        }

        public void setText(TextMsgVo text) {
            this.text = text;
        }

        public AtVo getAt() {
            return at;
        }

        public void setAt(AtVo at) {
            this.at = at;
        }

        public void setMarkdownMsg(String title, String text, AtVo at) {
            this.markdown = new MarkdownMsgVo();
            this.msgtype = "markdown";
            this.markdown.setText(text);
            this.markdown.setTitle(title);
            this.at = at;
        }

        public void setTextMsg(String text) {
            this.text = new TextMsgVo();
            this.msgtype = "text";
            this.text.setContent(text);
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
        }

        public static class MarkdownMsgVo {
            private String title;
            private String text;

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getText() {
                return text;
            }

            public void setText(String text) {
                this.text = text;
            }

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
            }
        }

        public static class TextMsgVo {
            private String content;

            public String getContent() {
                return content;
            }

            public void setContent(String content) {
                this.content = content;
            }

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
            }
        }

        public static class AtVo {
            @JSONField(name = "isAtAll")
            private boolean isAtAll;
            private List<String> atMobiles;

            public boolean isAtAll() {
                return isAtAll;
            }

            public void setIsAtAll(boolean isAtAll) {
                this.isAtAll = isAtAll;
            }

            public List<String> getAtMobiles() {
                return atMobiles;
            }

            public void setAtMobiles(List<String> atMobiles) {
                this.atMobiles = atMobiles;
            }

            @Override
            public String toString() {
                return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
            }
        }
    }
}
